﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using DM_Article.Model;
using DM_Article.Bll;

public partial class admin_admin_bigclass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
            else
            {
                getbigclass();
            }
        }
    }

    //权限管理
    private bool checkUserType(string username)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            cn.Open();
            admin adn = adminbll.getAdminDetailByName(cn, username);
            return (adn.usertype == "0" || adn.usertype == "1");
        }
    }

    private void getbigclass()
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            this.GV_bigclass.DataSource = bigclassbll.getBigclassTable(cn).DefaultView;
            this.GV_bigclass.DataBind();
        }
    }
    protected void GV_bigclass_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            this.GV_bigclass.PageIndex = e.NewPageIndex;
            getbigclass();
        }
        catch
        {

        }
    }

    protected void DlPage_Init(object sender, EventArgs e)//DlPage就是分页中的下拉列表框
    {
        DropDownList list = (DropDownList)sender;
        for (int i = 1, j = GV_bigclass.PageCount; i <= j; i++)
        {
            list.Items.Add(new ListItem(i.ToString()));
        }
        list.SelectedIndex = GV_bigclass.PageIndex;
    }

    protected void DlPage_SelectedIndexChanged(object sender, EventArgs e)//DlPage就是分页中的下拉列表框
    {
        DropDownList list = (DropDownList)sender;
        GV_bigclass.PageIndex = list.SelectedIndex;
        getbigclass();
    }

    protected void GV_bigclass_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (!checkUserType(Session["username"].ToString()))
        {
            Response.Write("<script language='javascript'>alert('权限不足！');</script>");
            return;
        }
        else
        {
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
            {
                cn.Open();
                string bigclassid = this.GV_bigclass.Rows[e.RowIndex].Cells[0].Text; if (articlebll.deleteByBigclass(cn, bigclassid) > -1)
                {
                    if (smallclassbll.deleteByBigclass(cn, bigclassid) > -1)
                    {
                        if (bigclassbll.delete(cn, bigclassid) > 0)
                        {
                            Response.Write("<script language='javascript'>alert('删除成功！');location.href = 'admin_bigclass.aspx';</script>");
                        }
                        else
                        {
                            Response.Write("<script language='javascript'>alert('删除失败！');location.href = 'admin_bigclass.aspx';</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('删除[该大类下的小类]失败！');location.href = 'admin_bigclass.aspx';</script>");
                    }
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('删除[该大类下的文章失败]失败！');location.href = 'admin_bigclass.aspx';</script>");
                }
            }
        }
    }


    protected void GV_bigclass_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (!checkUserType(Session["username"].ToString()))
        {
            Response.Write("<script language='javascript'>alert('权限不足！');</script>");
        }
        else
        {
            string bigclassid = this.GV_bigclass.Rows[e.NewEditIndex].Cells[0].Text;
            Response.Redirect("admin_bigclassedit.aspx?bigclassid=" + bigclassid);
        }
    }
}
